package com.lemon.boot.common.utils;

import org.springframework.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 李猛
 * @datetime 2024/6/10 16:59
 * @description 验证工具类-正则表达式
 */
public final class ValidateUtil {
    //手机号码正则表达式
    private static final Pattern PHONE_NUMBER_PATTERN = Pattern.compile("^1[3-9]\\d{9}$");

    //固话正则表达式
    private static final Pattern TEL_NUMBER_PATTERN = Pattern.compile("^\\(?(\\d{2,3})\\)?[\\s-]?(\\d{7,8})$");

    //E-mail 正则表达式
    private static final Pattern EMAIL_PATTERN = Pattern.compile("^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$");

    //数字正则表达式
    private static final Pattern NUMBER_PATTERN = Pattern.compile("\\d+");

    /**
     * 正则表达式匹配手机号
     *
     * @param phone
     * @return
     */
    public static boolean isPhone(String phone) {
        if(!StringUtils.hasText(phone)){
            return false;
        }
        return PHONE_NUMBER_PATTERN.matcher(phone).matches();
    }

    /**
     * 正则表达式匹配固话
     *
     * @param tel
     * @return
     */
    public static boolean isTel(String tel) {
        if(!StringUtils.hasText(tel)){
            return false;
        }
        return TEL_NUMBER_PATTERN.matcher(tel).matches();
    }

    /**
     * 正则表达式匹配邮箱
     *
     * @param email
     * @return
     */
    public static boolean isEmail(String email) {
        if(!StringUtils.hasText(email)){
            return false;
        }
        return EMAIL_PATTERN.matcher(email).matches();
    }

    /**
     * 正则表达式匹配邮箱
     *
     * @param number
     * @return
     */
    public static boolean isNumber(String number) {
        if(!StringUtils.hasText(number)){
            return false;
        }
        return NUMBER_PATTERN.matcher(number).matches();
    }

    public static void main(String[] args) {
        String[] phones = {
                "021-88888888",
                "(021)88888888",
                "(021) 88888888",
                "021 88888888",
                "02188888888",
                "021-888-8888",
                "021 888-8888",
                "(021) 888-8888"
        };
        for (String phone : phones) {
            if (isTel(phone)) {
                System.out.println(phone + " is a valid phone number.");
            } else {
                System.out.println(phone + " is not a valid phone number.");
            }
        }
    }
}
